// components/classic/music/index.js
import { classicbeh } from "../classic-beh.js";

const mMgr = wx.getBackgroundAudioManager();
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [classicbeh],
  properties: {
      src:String
  },

  /**
   * 组件的初始数据
   */
  data: {
    playing:false,
    playSrc:"images/player@playing.png",
    pauseSrc:"images/player@waitting.png"
  },
  attached:function(){
    this._resverStatus();
    this._monitorSwitch();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onPlay:function(){
      if(this.data.playing)
      {
        this.setData({
          playing: false
        });
        mMgr.pause();
      }else{
        this.setData({
          playing: true
        });
        mMgr.src = this.properties.src;
      }
    },
    _resverStatus:function(){  //判断当前音乐的状态
        if(mMgr.paused)  //判断是否是暂停状态
        {
            this.setData({
              playing:false
            });
            return;
        }
        if(mMgr.src == this.properties.src)  //判断当前播放的和当前组件是否一样
        {
            this.setData({
              playing:true
            })
        }
    },
    _monitorSwitch:function(){  //判断总控开关的状态
      mMgr.onPlay(()=>{
        this._resverStatus();
      });
      mMgr.onPause(() => {
        this._resverStatus();
      });
      mMgr.onStop(() => {
        this._resverStatus();
      });
      mMgr.onEnded(() => {  //当一首歌自然播放完成
        this._resverStatus();
      });
    }
  }
})
